package org.duang.db.sql.event.monitor;

import org.duang.db.sql.event.SqlEvent;
import org.duang.event.core.EventListener;
import org.duang.kit.SqlKit;
import org.duang.kit.ThreadPoolKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SqlListener implements EventListener<SqlEvent, Object>{

	private final static Logger logger = LoggerFactory.getLogger(SqlListener.class);
	
	@Override
	public Object onEvent(SqlEvent event) {
		final Class<?> cls = (Class<?>)event.getSource();
		ThreadPoolKit.execute(new Thread(){
			public void run() {
				try {
					SqlKit.createTable(cls);
					SqlKit.createIndex(cls);
				} catch (Exception e) {
					logger.warn("init "+cls.getName()+" table fail: " + e.getMessage(), e);
				}		
			}
		});
		return null;
	}

	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public String getKey() {
		return "Duang_Sql";
	}

}
